package com.tsingteng.cosfun.ui.message;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.NotificationBean;
import com.tsingteng.cosfun.ui.message.chat.ChatManyAdapter;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.SPUtils;
import com.tsingteng.cosfun.utils.TimeUtils;


import java.text.ParseException;
import java.util.Date;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class MessageAdapter extends BaseQuickAdapter<NotificationBean.ListBean,BaseViewHolder> {

    private TextView tv_message_item_count;

    public  MessageAdapter() {
        super(R.layout.item_message_adapter);
    }
    @Override
    protected void convert(BaseViewHolder helper, NotificationBean.ListBean item) {
        tv_message_item_count = helper.getView(R.id.tv_message_item_count);
        if(item.getTarget().getId() == SPUtils.init().getInt("profile_id")){
            NotificationBean.ListBean.SourceBean source = item.getSource();
            if(source== null)return;
                helper.setText(R.id.tv_message_title,source.getNickName()+"");
                CircleImageView view = helper.getView(R.id.iv_message_header);
                ImageUtils.LoadImage(mContext,source.getImage(),view);
            helper.setText(R.id.tv_message_content,item.getContent());
            long createTime = item.getCreateTime();
            if(createTime < (60 * 1000 * 5)){
                helper.getView(R.id.tv_message_time).setVisibility(View.INVISIBLE);
            }else{
                helper.setText(R.id.tv_message_time, TimeUtils.formatChatTime(new Date(createTime)));
            }
        }else if(item.getSource().getId() == SPUtils.init().getInt("profile_id")){
            NotificationBean.ListBean.TargetBean target = item.getTarget();
            if(target== null)return;
                helper.setText(R.id.tv_message_title,target.getNickName()+"");
                CircleImageView view = helper.getView(R.id.iv_message_header);
                ImageUtils.LoadImage(mContext,target.getImage(),view);

            helper.setText(R.id.tv_message_content,item.getContent());
            long createTime = item.getCreateTime();
            if(createTime < (60 * 1000 * 5)){
                helper.getView(R.id.tv_message_time).setVisibility(View.INVISIBLE);
            }else{
                helper.setText(R.id.tv_message_time, TimeUtils.formatChatTime(new Date(createTime)));
            }
        }else if(item.getSource().getId() == 0){
            helper.setText(R.id.tv_message_title,"系统通知");
            CircleImageView view = helper.getView(R.id.iv_message_header);
            ImageUtils.LoadImage(mContext,R.drawable.news_notice_icon,view);

            helper.setText(R.id.tv_message_content,"系统通知内容");
            long createTime = AppUtils.getNotificationTime();
            boolean isYesterday = false;
            try {
                isYesterday = TimeUtils.IsYesterday(createTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (isYesterday){
                helper.setText(R.id.tv_message_time, mContext.getResources().getString(R.string.yesterday));
            }else {
                helper.setText(R.id.tv_message_time, TimeUtils.formatChatTime(new Date(createTime)));
            }
        }else if(item.getSource().getId() == 1){
            helper.setText(R.id.tv_message_title,"官方助手");
            CircleImageView view = helper.getView(R.id.iv_message_header);
            ImageUtils.LoadImage(mContext,R.drawable.news_logo_icon,view);

            helper.setText(R.id.tv_message_content,"官方助手内容");
            long createTime = AppUtils.getOfficialTime();
            boolean isYesterday = false;
            try {
                isYesterday = TimeUtils.IsYesterday(createTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (isYesterday){
                helper.setText(R.id.tv_message_time,mContext.getResources().getString(R.string.yesterday));
            }else {
                helper.getView(R.id.tv_message_time).setVisibility(View.VISIBLE);
                helper.setText(R.id.tv_message_time, TimeUtils.formatChatTime(new Date(createTime)));
            }
        }

        //未读次
        int unreadCount = item.getUnreadCount();
        if(unreadCount == 0){
            tv_message_item_count.setVisibility(View.INVISIBLE);
        }else if(unreadCount > 99){
            tv_message_item_count.setVisibility(View.VISIBLE);
            tv_message_item_count.setText("99+");
        }else{
            tv_message_item_count.setVisibility(View.VISIBLE);
            tv_message_item_count.setText(unreadCount+"");
        }

        ImageView iv_message_header = helper.getView(R.id.iv_message_header);

            iv_message_header.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int adapterPosition = helper.getAdapterPosition();
                    if (adapterPosition ==1 || adapterPosition == 2) return;
                        int tag = 1;
                        Bundle bundle = new Bundle();
                        bundle.putInt("tag", tag);
                    int id = item.getTarget().getId();
                    int user_id;
                    if(id == SPUtils.init().getInt("profile_id")){
                        user_id = item.getSource().getId();
                    }else{
                        user_id = id;
                    }
                    bundle.putInt("user_id",user_id);
                        TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
                    }
            });
    }
}
